package com.streetsmart;

import java.util.Calendar;
import com.streetsmart.R;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;

import com.streetsmart.main.SSMainActivity;
import com.streetsmart.test.TestActivity;

public class MainActivity extends Activity {
	//public static final long ALARM_TRIGGER_AT_TIME = 
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		DBManager dbManager = new DBManager(this);
		dbManager.setFirstRecentLocation();
		dbManager.setLastRecentLocation();
		Context context = getApplicationContext();
		Calendar time = Calendar.getInstance();
		time.set(Calendar.HOUR_OF_DAY, 14);
		time.set(Calendar.MINUTE, 9);
		time.set(Calendar.SECOND, 0);
		AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, AlarmReceiver.class);
		intent.putExtra("PERIODIC", "true");
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		Log.i("StreetSmart-Log", "AlarmManager invoked");
		alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 60 * 1000, pendingIntent);
		checkLocationService();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options, menu);
        return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.mnuTest:
			Intent intent = new Intent(this, TestActivity.class);
			startActivity(intent);
			return true;
		case R.id.mnumain:
			Intent i = new Intent(this, SSMainActivity.class);
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * triggers location service to constantly update locations
	 */
	private void checkLocationService()
	{
/*	    Log.d("CHECK_SERVICE", "Service running: " + (settings.getBoolean("locationService", false)?"YES":"NO"));

	    if(settings.getBoolean("locationService", false))
	        return;*/

	    Intent serviceIntent = new Intent(this, LocationService.class);
	    startService(serviceIntent);
	}
	
}
